package pe.gob.pnp.servlets;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletOutputStream;
import org.apache.log4j.Logger;
import pe.gob.pnp.actions.DetalleResolucionBean;
import pe.gob.pnp.actions.DictamenBean;
import pe.gob.pnp.actions.ResolucionBean;
import pe.gob.pnp.despachadores.DictamenDespachador;
import pe.gob.pnp.despachadores.ResolucionDespachador;
import pe.gob.pnp.digitalizar.beans.DictamenDetalle;
import pe.gob.pnp.digitalizar.beans.PersonalBean;
import pe.gob.pnp.digitalizar.despachadores.PersonalDespachador;
import pe.gob.pnp.libreria.Utiles;

/**
 *
 * @author ECAMPOS
 */
public class PdfResolucion {

    private static final Logger logger = Logger.getLogger(PdfResolucion.class);
    private Document  document;
    private BaseFont bf;
    private Font arial11;
    private Font arial07;

    public Document crearPdfResolucion(ServletOutputStream out,String nres_identificador){
        document = new Document(PageSize.A4);
        inicializaFuentes();
         try{

            ResolucionBean bresolucion = ResolucionDespachador.obtenerResolucionById(Integer.parseInt(nres_identificador));
            PersonalBean policia = PersonalDespachador.obtenerPersonalPolicial( bresolucion.getNpepo_identificador() );
           

            PdfWriter.getInstance(document, out);
            //Izquierda, Derecha, Superior, Inferior
            //document.setMargins(5,5,50,50) ;
            document.setMargins (40,-10,50,50);
            document.setMarginMirroring(true);
            //document.setMargins(0.5f,2.5f,1.5f,1.0f) ;
            document.open();

            crearCabecera(bresolucion, policia);
            crearCuerpo(bresolucion);
            crearPie(bresolucion);

            document.close();

        }catch(Exception e){
            e.printStackTrace();
        }
        return document;
    }

    private void crearCabecera(ResolucionBean bresolucion, PersonalBean policia) {
        try {
           
            PdfPTable table = new PdfPTable(4);
            PdfPCell cell;

            
            cell = new PdfPCell(new Phrase(""));
            cell.setColspan(2);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setBorder(0);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(bresolucion.getCreso_sumilla(),arial07));
            cell.setColspan(2);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setBorder(0);
            table.addCell(cell);

            Chunk chunk = new Chunk("N° "+ bresolucion.getNreso_numero() + " - " + Utiles.DateToString( bresolucion.getDreso_fecha(), "yyyy") +" - DIRPEN-PNP,  Lima " + Utiles.DateToString( bresolucion.getDreso_fecha(), "dd MMM yyyy")  ,arial11);
            //chunk.setUnderline(0.2f, -2f);
            Paragraph paragraph = new Paragraph("");
            paragraph.add(chunk);

            cell = new PdfPCell(paragraph);
            //aumentar valor de paddin para aumentar espacio arriba
            cell.setPaddingTop(80f);
            //para aumetnar espacio abajo
            cell.setPaddingBottom(15f);
            cell.setColspan(4);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase( "VISTO, " + bresolucion.getCreso_visto() ,arial11));
            cell.setColspan(4);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);

            document.add(table);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void crearCuerpo(ResolucionBean bresolucion) {
        try {
            PdfPTable table = new PdfPTable(4);
            PdfPCell cell;

            List<DetalleResolucionBean> uno = ResolucionDespachador.obtenerDetalleResolucion(bresolucion.getNreso_identificador(), 1);
            List<DetalleResolucionBean> dos = ResolucionDespachador.obtenerDetalleResolucion(bresolucion.getNreso_identificador(), 2);

            cell= new PdfPCell(new Phrase( "         CONSIDERANDO:" ,arial11));
            cell.setColspan(4);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);

            //Observaciones
            int numero = 0 ;
            for(DetalleResolucionBean obj : uno){
                cell= new PdfPCell(new Phrase( obj.getCderes_detalle() ,arial11));
                cell.setColspan(4);
                cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
                cell.setPadding(8f);
                cell.setBorder(0);
                table.addCell(cell);
            }

            cell= new PdfPCell(new Phrase( "          SE RESUELVE:" ,arial11));
            cell.setColspan(4);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);

            //resuelve
            numero = 1 ;
            for(DetalleResolucionBean obj : dos){
                cell= new PdfPCell(new Phrase( "Artículo " + numero + "° .- " +  obj.getCderes_detalle() ,arial11));

                cell.setColspan(4);
                cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
                cell.setPadding(10f);
                cell.setBorder(0);
                table.addCell(cell);
                numero++;
            }
             document.add(table);
        } catch (Exception e) {
            e.printStackTrace();
        }
       
    }

    private void crearPie(ResolucionBean bresolucion) {
        try {
            PdfPTable table = new PdfPTable(4);
            PdfPCell cell;

            cell= new PdfPCell(new Phrase( "Regístrece, comuníquese y archívese" ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setColspan(4);
            cell.setPadding(10f);
            cell.setPaddingTop(30f);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase(bresolucion.getCreso_siglas() ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setColspan(4);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);


            document.add(table);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void inicializaFuentes() {
        bf = null;
        try {
            bf = BaseFont.createFont(BaseFont.RESOURCE_PATH +"arial.ttf", BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
        } catch (Exception ex) {
           logger.error(ex.getMessage());
        }
        arial11 = new Font(bf, 12);
        arial07 = new Font(bf, 8);
    }

}
